﻿using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace FirmaTransportowa
{
    public class Sender : IModel<Sender>
    {
        [System.ComponentModel.DisplayName("ID wysyłającego")]
        public int id { get; set; }
        [System.ComponentModel.DisplayName("Imię")]
        public String name { get; set; }
        [System.ComponentModel.DisplayName("Nazwisko")]
        public String surname { get; set; }
        [System.ComponentModel.DisplayName("Kod pocztowy")]
        public String postcode { get; set; }
        [System.ComponentModel.DisplayName("Miasto")]
        public String city { get; set; }
        [System.ComponentModel.DisplayName("Ulica")]
        public String street { get; set; }
        [System.ComponentModel.DisplayName("Numer domu")]
        public String house_number { get; set; }
        [System.ComponentModel.DisplayName("Numer telefonu")]
        public String phone { get; set; }

        public String displayName
        {
            get { return name + " " + surname; }
        }

        public Sender(int id, string name, string surname, string postcode, string city, string street, string house_number, string phone)
        {
            this.id = id;
            this.name = name;
            this.surname = surname;
            this.postcode = postcode;
            this.city = city;
            this.street = street;
            this.house_number = house_number;
            this.phone = phone;
        }

        public Sender()
        {
        }

        public string DisplayText
        {
            get
            {
                return this.name + " " + this.surname;
            }
        }

        public int ReturnValue
        {
            get
            {
                return this.id;
            }
        }

        public static int getIdByNameAndSurname(string name, string surname) {
            SqlConnection conn = SqlConnectionFactory.getConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT sender_ID FROM Sender WHERE name = '" + name + "' AND surname = '" + surname + "' ", conn);
                SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.Default);

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        return int.Parse(reader["sender_ID"].ToString());
                    }
                }
                reader.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + e.StackTrace);
            }
            finally
            {
                conn.Close();
            }

            return -1;
        }

        public int save()
        {
            SqlConnection conn = SqlConnectionFactory.getConnection();

            int createdId = -1;

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                bool newObject = false;

                if (Sender.getById(this.id) == null)
                {
                    cmd.CommandText = "INSERT INTO Sender(name,surname,postcode,city,street,house_number,phone) VALUES(@name, @surname, @postcode, @city, @street, @house_number, @phone) SELECT CONVERT(int, SCOPE_IDENTITY())";
                    newObject = true;
                }
                else 
                {
                    cmd.CommandText = "UPDATE Sender SET name=@name, surname=@surname, postcode=@postcode, city=@city, street=@street, house_number=@house_number, phone=@phone WHERE sender_ID=@id";
                    cmd.Parameters.AddWithValue("@id", this.id);
                }
                cmd.Parameters.AddWithValue("@name", this.name);
                cmd.Parameters.AddWithValue("@surname", this.surname);
                cmd.Parameters.AddWithValue("@postcode", this.postcode);
                cmd.Parameters.AddWithValue("@city", this.city);
                cmd.Parameters.AddWithValue("@street", this.street);
                cmd.Parameters.AddWithValue("@house_number", this.house_number);
                cmd.Parameters.AddWithValue("@phone", this.phone);
                cmd.Connection = conn;

                if (newObject)
                {
                    createdId = (int)cmd.ExecuteScalar();
                }
                else
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception e)
            {

            }

            return createdId;
        }

        public static IList<Sender> list()
        {
            SqlConnection conn = SqlConnectionFactory.getConnection();

            IList<Sender> list = new List<Sender>();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT * FROM Sender", conn);
                SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.Default);

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Sender client = Sender.getById(int.Parse(reader["sender_ID"].ToString()));

                        list.Add(client);
                    }
                }
                reader.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + e.StackTrace);
            }
            finally
            {
                conn.Close();
            }

            return list;
        }


        public static Sender getById(int id)
        {
            SqlConnection conn = SqlConnectionFactory.getConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "SELECT * FROM Sender WHERE sender_ID=@id;";
                cmd.Parameters.AddWithValue("@id", id);
                cmd.Connection = conn;
                SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow);

                if (reader.HasRows)
                {
                    reader.Read();
                    Sender client = new Sender();
                    client.id = int.Parse(reader["sender_ID"].ToString());
                    client.name = reader["name"].ToString();
                    client.surname = reader["surname"].ToString();
                    client.postcode = reader["postcode"].ToString();
                    client.city = reader["city"].ToString();
                    client.street = reader["street"].ToString();
                    client.house_number = reader["house_number"].ToString();
                    client.phone = reader["phone"].ToString();

                    conn.Close();
                    return client;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n" + e.StackTrace);
            }
            finally
            {
                conn.Close();

            }

            return null;

        }

        public void delete()
        {
            SqlConnection conn = SqlConnectionFactory.getConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "DELETE FROM Sender WHERE client_ID=@id";
                cmd.Parameters.AddWithValue("@id", this.id);

                cmd.Connection = conn;
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n" + e.StackTrace);
            }
            finally
            {
                conn.Close();
            }
        }

        public static Predicate<Sender> ById(int id)
        {
            return delegate(Sender client)
            {
                return client.id == id;
            };
        }
    }
}
